package com.zzyl.controller;


import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
@Api(tags = "角色管理接口")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    public ResponseResult<PageResponse<RoleVo>> getByPage(@RequestBody RoleDto roleDto,
                                                          @PathVariable Integer pageNum,
                                                          @PathVariable Integer pageSize){

        PageResponse<RoleVo> page=roleService.getByPage(roleDto,pageSize,pageNum);
        return ResponseResult.success(page);
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色查询选中的资源数据")
    public ResponseResult<List<String>> getCheckResourceNo(@PathVariable Long roleId){
        List<String> list=roleService.getCheckResourceNo(roleId);
        return ResponseResult.success(list);
    }

    @PatchMapping
    @ApiOperation("角色修改")
    public ResponseResult updateRole(@RequestBody RoleDto roleDto){
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/{roleId}")
    @ApiOperation("删除角色")
    public ResponseResult delete(@PathVariable Long roleId){
        roleService.delete(roleId);
        return ResponseResult.success();
    }

    @PutMapping
    @ApiOperation( "添加角色")
    public ResponseResult add(@RequestBody RoleDto roleDto){
        roleService.add(roleDto);
        return ResponseResult.success();
    }

    @PostMapping("/init-roles")
    @ApiOperation("查询所有")
    public ResponseResult<List<RoleVo>> getAll(){
        List<RoleVo> list =roleService.getAll();
        return ResponseResult.success(list);
    }
}
